import re

from django_redis import get_redis_connection
from rest_framework import serializers

from goods.models import SKU
from users import contants
from users.models import User
from users.models import Address
from celery_tasks.email.task import send_active_email
class CreateUserSerializer(serializers.ModelSerializer):
    password2 = serializers.CharField(label='确认密码', write_only=True)
    sms_code = serializers.CharField(label='短信验证码', write_only=True)
    allow = serializers.CharField(label='同意协议', write_only=True)
    token = serializers.CharField(label="JWT token",read_only=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'password2', 'sms_code', 'mobile', 'allow',"token")
        extra_kwargs = {
            'username': {
                'min_length': 5,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许5-20个字符的用户名',
                    'max_length': '仅允许5-20个字符的用户名',
                }
            },
            'password': {
                'write_only': True,
                'min_length': 8,
                'max_length': 20,
                'error_messages': {
                    'min_length': '仅允许8-20个字符的密码',
                    'max_length': '仅允许8-20个字符的密码',
                }
            }
        }

    def validate_mobile(self,value):

        if not re.match(r'1[3-9]\d{9}$',value):
            raise serializers.ValidationError("手机号码出错")
        return value

    def validate_allow(self,value):

        if value != "true":
            raise serializers.ValidationError("用户协议未同意")
        return value

    def validate(self,data):


        if data["password"] !=data["password2"]:
            raise serializers.ValidationError("密码不统一")

        # 短信验证码
        redis_conn = get_redis_connection("verify_codes")
        mobile = data["mobile"]
        real_sms_code = redis_conn.get('sms_%s' % mobile)
        if real_sms_code is None:
            raise serializers.ValidationError('无效的短信验证码')
        if data['sms_code'] != real_sms_code.decode():
            raise serializers.ValidationError('短信验证码错误')

        return data

    def create(self, validated_data):
        validated_data.pop('password2')
        validated_data.pop('sms_code')
        validated_data.pop('allow')
        user = super().create(validated_data)
        user.save()

        from rest_framework_jwt.settings import api_settings

        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(user)
        token = jwt_encode_handler(payload)

        user.token = token
        return user

#
#
class UserDetailSerializer(serializers.ModelSerializer):
    """
       用户详细信息序列化器
       """

    class Meta:
        model = User
        fields = ('id', 'username', 'mobile', 'email', 'email_active')




class EmailSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("id","email")
        extra_kwargs = {
            'email': {
                'required': True
            }
        }

    def update(self, instance, validated_data):

        email = validated_data["email"]

        instance.email = email
        instance.save()


        url = instance.generate_verify_email_url()
        print(".."+url)
        print(email)
        send_active_email.delay(email,url)

        return instance

class UserAddressSerializer(serializers.ModelSerializer):
    province = serializers.StringRelatedField(read_only=True)
    city = serializers.StringRelatedField(read_only=True)
    district = serializers.StringRelatedField(read_only=True)
    province_id = serializers.IntegerField(label='省ID', required=True)
    city_id = serializers.IntegerField(label='市ID', required=True)
    district_id = serializers.IntegerField(label='区ID', required=True)

    class Meta:
        model = Address
        exclude = ('user', 'is_deleted', 'create_time', 'update_time')

    def validate_mobile(self, value):
        if not re.match(r'^1[3-9]\d{9}$', value):
            raise serializers.ValidationError("手機格式錯誤")
        return value

    def create(self, validated_data):

        validated_data["user"] = self.context["requset"].user
        return super().create(validated_data)

class AddressTitleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        filter = ("title")


class AddUserrHistorySerializer(serializers.Serializer):
    sku_id = serializers.IntegerField(label="商品SKU编号", min_value=1)

    def validate_sku_id(self, value):
        """
        检验sku_id是否存在
        """
        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError('该商品不存在')
        return value

    def create(self, validated_data):
        """
        保存
        """
        user_id = self.context['request'].user.id
        sku_id = validated_data['sku_id']

        redis_conn = get_redis_connection("history")
        pl = redis_conn.pipeline()

        # 移除已经存在的本商品浏览记录
        pl.lrem("history_%s" % user_id, 0, sku_id)
        # 添加新的浏览记录
        pl.lpush("history_%s" % user_id, sku_id)
        # 只保存最多5条记录
        pl.ltrim("history_%s" % user_id, 0, contants.USER_BROWSING_HISTORY_COUNTS_LIMIT - 1)

        pl.execute()

        return validated_data

class SKUSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'default_image_url', 'comments')
